package com.castle.fortress.admin.flowable.form.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.flowable.form.dto.FlowableFormConfigDto;
import com.castle.fortress.admin.flowable.form.entity.FlowableFormConfigEntity;
import com.castle.fortress.admin.flowable.form.service.FlowableFormConfigService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 表单配置管理 api 控制器
 *
 * @author castle
 * @since 2022-09-27
 */
@Api(tags="表单配置管理api管理控制器")
@RestController
@RequestMapping("/api/flowable/form/flowableFormConfig")
public class ApiFlowableFormConfigController {
    @Autowired
    private FlowableFormConfigService flowableFormConfigService;


    /**
     * 表单配置管理的分页展示
     * @param flowableFormConfigDto 表单配置管理实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("表单配置管理分页展示")
    @GetMapping("/page")
    public RespBody<IPage<FlowableFormConfigDto>> pageFlowableFormConfig(FlowableFormConfigDto flowableFormConfigDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<FlowableFormConfigDto> page = new Page(pageIndex, pageSize);

        IPage<FlowableFormConfigDto> pages = flowableFormConfigService.pageFlowableFormConfig(page, flowableFormConfigDto);
        return RespBody.data(pages);
    }

    /**
     * 表单配置管理保存
     * @param flowableFormConfigDto 表单配置管理实体类
     * @return
     */
    @ApiOperation("表单配置管理保存")
    @PostMapping("/save")
    public RespBody<String> saveFlowableFormConfig(@RequestBody FlowableFormConfigDto flowableFormConfigDto){
        if(flowableFormConfigDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            FlowableFormConfigEntity flowableFormConfigEntity = ConvertUtil.transformObj(flowableFormConfigDto,FlowableFormConfigEntity.class);
        if(flowableFormConfigService.save(flowableFormConfigEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 表单配置管理编辑
     * @param flowableFormConfigDto 表单配置管理实体类
     * @return
     */
    @ApiOperation("表单配置管理编辑")
    @PostMapping("/edit")
    public RespBody<String> updateFlowableFormConfig(@RequestBody FlowableFormConfigDto flowableFormConfigDto){
        if(flowableFormConfigDto == null || flowableFormConfigDto.getId() == null || flowableFormConfigDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            FlowableFormConfigEntity flowableFormConfigEntity = ConvertUtil.transformObj(flowableFormConfigDto,FlowableFormConfigEntity.class);
        if(flowableFormConfigService.updateById(flowableFormConfigEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 表单配置管理删除
     * @param ids 表单配置管理id集合
     * @return
     */
    @ApiOperation("表单配置管理删除")
    @PostMapping("/delete")
    public RespBody<String> deleteFlowableFormConfig(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(flowableFormConfigService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 表单配置管理详情
     * @param id 表单配置管理id
     * @return
     */
    @ApiOperation("表单配置管理详情")
    @GetMapping("/info")
    public RespBody<FlowableFormConfigDto> infoFlowableFormConfig(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            FlowableFormConfigEntity flowableFormConfigEntity = flowableFormConfigService.getById(id);
            FlowableFormConfigDto flowableFormConfigDto = ConvertUtil.transformObj(flowableFormConfigEntity,FlowableFormConfigDto.class);
        return RespBody.data(flowableFormConfigDto);
    }


}
